import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subject, takeUntil } from 'rxjs';
import { Store } from '@ngxs/store';
import { SecondStatNamePipe } from '../../shared/pipe/second-stat-name.pipe';
import { SaveAction, SaveSelector } from '../../store/save';
import * as _ from 'lodash';

@Component({
  selector: 'app-soul',
  templateUrl: './soul.component.html',
  styleUrls: ['./soul.component.scss'],
})
export class SoulComponent implements OnInit, OnDestroy {
  temp: any = [];
  data: any = [];
  filtersName: any[] = [];
  filtersStat3: any[] = [];
  private destroy: Subject<boolean> = new Subject<boolean>();

  constructor(
      private store: Store,
      private secondStatNamePipe: SecondStatNamePipe,
  ) {
    this.store.select(SaveSelector.data).pipe(takeUntil(this.destroy)).subscribe(r => {
      this.temp = r['玩家器魂'] as any[];
      this.data = this.temp;
      this.load();
    });
  }

  ngOnInit(): void {
    this.store.dispatch(new SaveAction.Load());
    this.load();
  }

  load() {
    console.log('data', this.temp);
    this.filterName();
    this.filterStat3();
  }

  filterName() {
    const filtersName = _(this.data).map(s => s['名称']).uniq().sortBy(s => s).map(s => {
      return {text: s, value: s};
    }).value();
    if (!_.eq(JSON.stringify(filtersName), JSON.stringify(this.filtersName)).valueOf()) {
      this.filtersName = filtersName;
    }
  }

  filterStat3() {
    const filterStat3 = _(this.data).map(s => s['随机属性链表'][0]?.['二级属性']).uniq().sortBy(s => s).map(s => {
      return {text: this.secondStatNamePipe.transform(s), value: s};
    }).value();
    if (!_.eq(JSON.stringify(filterStat3), JSON.stringify(this.filtersStat3)).valueOf()) {
      this.filtersStat3 = filterStat3;
    }
  }

  filterNameFn = (list: any[], item: any) => {
    return list.some(v => v === item['名称']);
  };
  filterStat3Fn = (list: any[], item: any) => {
    return list.some(v => v === item['随机属性链表'][0]?.['二级属性']);
  };

  sortNameFn = (a: any, b: any) => {
    return a['名称'].localeCompare(b['名称']);
  };
  sortStat3Fn = (a: any, b: any) => {
    return a['随机属性链表'][0]?.['属性数值'] > (b['随机属性链表'][0]?.['属性数值']) ? 1 : -1;
  };
  sortPriceFn = (a: any, b: any) => {
    return a['价值'] > b['价值'] ? 1 : -1;
  };
  sortLevelFn = (a: any, b: any) => {
    return a['等级'] > b['等级'] ? 1 : -1;
  };

  sell(data: any) {
    this.store.dispatch(new SaveAction.SellItem(data)).subscribe(() => {
      this.store.dispatch(new SaveAction.Save());
    });
  }

  ngOnDestroy(): void {
    this.destroy.next(true);
    this.destroy.unsubscribe();
  }

  findColor(data: any) {
    let color = '';
    if (6 === data['品质']) {
      color = 'red';
    } else if (5 === data['品质']) {
      color = 'orange';
    } else if (4 === data['品质']) {
      color = 'purple';
    } else if (3 === data['品质']) {
      color = '#00b8ff';
    } else if (2 === data['品质']) {
      color = 'green';
    } else if (1 === data['品质']) {
      color = 'black';
    }
    return {'color': color};
  }

  changeLock(data: any) {
    this.store.dispatch(new SaveAction.LockItem(data)).subscribe(() => {
      this.store.dispatch(new SaveAction.Save());
    });
  }
}
